package com.first.nell.me

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.first.nell.R
import com.first.nell.base.BaseActivity
import com.first.nell.base.BaseFragment
import com.first.nell.components.dialog.CustomerServiceDialog
import com.first.nell.components.dialog.MessageDialog
import com.first.nell.data.ReqBase
import com.first.nell.databinding.FragmentFirstNellMeBinding
import com.first.nell.extend.startFirstNellToFirstNellLoginFirstNellActivity
import com.first.nell.extend.startFirstNellToFirstNellNextFirstNellActivity
import com.first.nell.extend.toFirstNellEncryptFirstNellPhoneFirstNellNumber
import com.first.nell.utils.UserStateUtil


class MeLoanRejectFragment : BaseFragment() {

    private lateinit var mFirstNellFragmentFirstNellBinding: FragmentFirstNellMeBinding
    private lateinit var mCustomerServiceAdapter: CustomerServiceAdapter

    val mMeViewModel by lazy {
        ViewModelProvider(this)[MeViewModel::class.java]
    }

    override fun onFirstNellBindingFirstNellViewFirstNellModel(): List<ViewModel> {
        val list = mutableListOf<ViewModel>()
        list.add(mMeViewModel)
        return list
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        mFirstNellFragmentFirstNellBinding =
            FragmentFirstNellMeBinding.inflate(inflater, container, false)
        return mFirstNellFragmentFirstNellBinding.root
    }

    @SuppressLint("NotifyDataSetChanged", "SetTextI18n")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        try {
            //初始化客服列表
            mCustomerServiceAdapter =
                CustomerServiceAdapter(mMeViewModel.mFirstNellCustomerFirstNellServiceFirstNellList)
            mFirstNellFragmentFirstNellBinding.rcMeCustomerServiceList.layoutManager =
                LinearLayoutManager(requireContext())
            mFirstNellFragmentFirstNellBinding.rcMeCustomerServiceList.adapter =
                mCustomerServiceAdapter

            //获取app配置回调
            mMeViewModel.onFirstNellFetchFirstNellContactFirstNellListFirstNellFromFirstNellCachedFirstNellLiveData.observe(
                viewLifecycleOwner
            ) {
                mCustomerServiceAdapter.notifyDataSetChanged()
            }

            //获取app配置回调
            mMeViewModel.onFirstNellFetchFirstNellContactFirstNellListFirstNellLiveData.observe(
                viewLifecycleOwner
            ) {
                if (it.isSuccess) {
                    it.getOrNull()?.let { result ->
                        mMeViewModel.mFirstNellCustomerFirstNellServiceFirstNellList.addAll(
                            result
                        )
                        mCustomerServiceAdapter.notifyDataSetChanged()
                    }
                }
            }

            //点击反馈
            mFirstNellFragmentFirstNellBinding.rlMeServiceFeedbackBg.setOnClickListener {
                (requireContext() as BaseActivity).startFirstNellToFirstNellNextFirstNellActivity<FeedbackActivity>()
            }

            //点击退出登录
            mFirstNellFragmentFirstNellBinding.rlMeLogoutBg.setOnClickListener {
                MessageDialog.showFirstNellDialog(
                    (requireContext() as BaseActivity).supportFragmentManager,
                    getString(R.string.first_nell_are_u_sure_to_log_out_of_the_app),
                    getString(R.string.first_nell_no),
                    getString(R.string.first_nell_yes)
                ) {
                    UserStateUtil.logoutFirstNell()
                    startFirstNellToFirstNellLoginFirstNellActivity()
                }
            }

            //初始化电话
            mFirstNellFragmentFirstNellBinding.textMeUserMobile.text =
                UserStateUtil.mFirstNellMobile.toFirstNellEncryptFirstNellPhoneFirstNellNumber()

            //点击客服
            mFirstNellFragmentFirstNellBinding.rlMeCustomerServiceRow.setOnClickListener {
                CustomerServiceDialog.showFirstNellDialog(requireActivity().supportFragmentManager)
            }

            //获取app配置
            mMeViewModel.fetchFirstNellContactFirstNellList(
                ReqBase(
                    showLoading = false
                )
            )

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}